package agataMeble.inspiration.browser 
{
	import agataMeble.inspiration.Config;
	import base.loader.Library;
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.loadingtypes.LoadingItem;
	import com.greensock.easing.Back;
	import com.greensock.TweenMax;
	import com.nuke.math.Math2;
	import com.nuke.text.FontTextField;
	import com.nuke.utils.ColorUtils;
	import flash.display.Bitmap;
	import flash.display.BlendMode;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.AntiAliasType;
	import msz.utils.MSZUtils;
	/**
	 * ...
	 * @author Bartłomiej JERZY
	 */
	public dynamic class Leaf extends Sprite
	{

		
		
		private var _bg								:Sprite = Library.getMovieClip("leafMc")
		private var _pattern						:Sprite
		private var _mask							:Sprite
		private var _cont							:Sprite = new Sprite()
		private var _leaf							:Sprite = new Sprite()
		
		
		private var _pick							:Sprite = new Sprite()
		
		private var _data							:Object 
		
		private var _num							:FontTextField = new FontTextField({font:"HelveticaNeue",size:11,color:0xffffff,bold:true},{antiAliasType:AntiAliasType.NORMAL, autoSize:"left"})
		
		private var _img							:Bitmap
		private var _t								:Number
		private var _color							:uint
		private var _enabled						:Boolean = true
		private var _selected						:Boolean = false
		
		private var _loader							:BulkLoader = BulkLoader.getLoader("browser")
		
		public function Leaf($data:Object) 
		{
			
			_data = $data
			
				
			init()
			loadImage()
		}
		
		private function loadImage():void
		{
			var _li:LoadingItem = _loader.add(Config.URL_SITE+_data.leaf)
			if (_li.isLoaded) {
				imageLoaded()
			}else {
				_li.addEventListener(Event.COMPLETE, imageLoaded)
				_loader.start()
			}
		}
		
		public function loadNow():void {
			if (!_img) {
				_loader.loadNow(_data.img)
			}
		}
		
		private function imageLoaded(e:Event=null):void {
			_img = new Bitmap(_loader.getBitmapData(Config.URL_SITE + _data.leaf),"auto",true)
			_img.x = 575
			_img.y = 55
			_img.rotation = 90+36
			
			_cont.addChild(_img)
		}
		
		private function init():void
		{

			_pattern = new SuperShit()
			_mask = new SuperMask()
			
			_cont.graphics.beginFill(0xffffff)
			_cont.graphics.drawRect(480,0,-400,300)
			_cont.graphics.beginFill(0xffffff)
			_cont.mask = _mask
			
			
			
			_leaf.addChild(_cont)
			_leaf.addChild(_mask)
			_leaf.addChild(_pattern)
			
			addChild(_bg)
			addChild(_leaf)
			
			
			addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler)
			addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler)
			addEventListener(MouseEvent.CLICK, mouseClickHandler)
			buttonMode = true
			
			mouseOutHandler()
		}
		
		private function mouseOverHandler(e:MouseEvent= null):void 
		{
			if(_enabled) over()
		}
		
		private function mouseOutHandler(e:MouseEvent= null):void 
		{
			if(_enabled) out()
		}
		
		private function mouseClickHandler(e:MouseEvent= null):void 
		{
			if(_enabled) dispatchEvent(new Event(Event.SELECT))
		}
				
		private function over():void {
			TweenMax.to(_mask, .2, { radius:480} )
			TweenMax.to(_pattern, .2, { radius:480 } )
			TweenMax.to(_cont,.2,{colorMatrixFilter:{saturation:1,colorize:0x660066, amount:0.1}})
		}
		
		private function out():void {
			TweenMax.killTweensOf(_mask)
			TweenMax.killTweensOf(_pattern)
			TweenMax.killTweensOf(_cont)
			TweenMax.killTweensOf(_leaf)
			TweenMax.to(_mask, .6, { radius:450,angle:11} )
			TweenMax.to(_pattern, .6, { radius:450,angle:11 } )
			TweenMax.to(_cont, .6, { colorMatrixFilter: { saturation:0, colorize:0x660066, amount:0.4,contrast:1 }} )
			TweenMax.to(_leaf,.6,{glowFilter:{remove:true,color:0xffcc99, alpha:0, blurX:20, blurY:20,strength:0}})
		}
		public function show():void {
			TweenMax.to(_mask, .2, { radius:480,delay:0.5} )
			TweenMax.to(_mask, .2, { angle:25,delay:0.7} )
			TweenMax.to(_pattern, .2, { radius:480,delay:0.5 } )
			TweenMax.to(_pattern, .2, { angle:25,delay:0.7,overwrite:false } )
			TweenMax.to(_cont,.4,{colorMatrixFilter:{saturation:1,contrast:1.1},delay:0.5})
			TweenMax.to(_leaf,.4,{glowFilter:{color:0xffcc99, alpha:1, blurX:20, blurY:20,strength:2},delay:0.5})
		}
		
		public function hide():void {
			out()
		}
		
		public function destroy():void {
			_loader.get("http://agatameble.eu"+_data.leaf).removeEventListener(Event.COMPLETE, imageLoaded)
			
			removeEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler)
			removeEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler)
			removeEventListener(MouseEvent.CLICK, mouseClickHandler)
			
			if(_img) _cont.removeChild(_img)
			_leaf.removeChild(_bg)
			_leaf.removeChild(_cont)
			_leaf.removeChild(_mask)
			_leaf.removeChild(_pattern)
			removeChild(_leaf)
			removeChild(_cont)
		}
		
		public function get selected():Boolean { return _selected; }
		
		public function set selected(value:Boolean):void 
		{
			_selected = value;
			
			select()
		}
		
		private function select():void
		{
			if (_selected) {
				show()
				_enabled = false
			}else {
				_enabled = true
				hide()
				
			}
		}
		
		
	}

}